import Vue from 'vue'
import App from './App.vue'
import router from './router'
import 'lib-flexible/flexible'
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueRouter from 'vue-router'
import {
  List,
  Popup,
  Toast,
  NavBar,
  Form,
  Field,
  CellGroup,
  button,
  Cell,
  SwipeCell
} from 'vant';
Vue.use(List)
Vue.use(SwipeCell)
Vue.use(Popup)
Vue.use(Toast)
Vue.use(NavBar)
Vue.use(Form)
Vue.use(Field)
Vue.use(CellGroup)
Vue.use(button)
Vue.use(Cell)

router.beforeEach((to, from, next) => {
  if (to.name == "My") {
    if (localStorage.getItem('isLogin')) {
      next()
    } else {
      next({
        name: 'Login'
      })
    }
  }
  next()
})
const originPush = VueRouter.prototype.push
// 覆盖原型链的push
VueRouter.prototype.push = function (location, resolve, reject) {
  // this:路由实例对象

  // 判断用户有没有传后面两个可选参数
  if (resolve || reject) {
    return originPush.call(this, location, resolve, reject)
  } else { // 用户只传了第一个参数
    /* 
    默认底层： catch()方法代码  throw err : 抛出异常
    吞掉报错原理： 重写catch()方法,把默认底层的 throw err给去掉，就不会抛出异常
    */
    return originPush.call(this, location).catch(err => {
      //  throw err
    })
  }
}

Vue.filter('strFormat', function (val) {
  let str = val.slice(4)
  let mouth = str.slice(0, 2)
  let day = str.slice(2)
  return (mouth >= 10 ? mouth : mouth.slice(1)) + '月' + (day >= 10 ? day : day.slice(1)) + '日'
})
Vue.use(VueAxios, axios)

Vue.config.productionTip = false
axios.defaults.baseURL = 'https://apis.netstart.cn/zhihudaily';

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')